package com.fdb.efp.edoc.api.rest;

import com.fdb.basic.framework.core.constant.ApplicationConstance;
import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.ctr.service.vo.CtrLoanContVO;
import com.fdb.efp.e4a.service.facade.SRoleDataRuleService;
import com.fdb.efp.edoc.service.bo.CfcaSignInfoVO;
import com.fdb.efp.edoc.service.facade.CfcaSignInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class CfcaSignInfoController extends AbstractController {

	@Autowired
	@Qualifier("cfcaSignInfoService")
	private CfcaSignInfoService cfcaSignInfoService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	/**
	 * 列表数据查询
	 * 
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value = "/cfca/sign/infos", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<CfcaSignInfoVO>> queryCfcaSignInfoAll(@RequestBody CfcaSignInfoVO vo) {
		// this.executeQueryList("queryAll", cfcaSignInfoService, new Object[]{vo});
		setUserInfoToVO(vo);
		List<CfcaSignInfoVO> outputVo = cfcaSignInfoService.queryAllOwner(vo);
		return getResponseData(outputVo);
	}

	/**
	 * 根据主键查询详情
	 * 
	 * @return
	 */
	@RequestMapping(value = "/cfca/sign/info/{applySeq}", method = RequestMethod.GET)
	public @ResponseBody ResponseData<CfcaSignInfoVO> queryByPk(@PathVariable("applySeq") String applySeq) {
		CfcaSignInfoVO inVo = new CfcaSignInfoVO();
		inVo.setApplySeq(applySeq);
		CfcaSignInfoVO outputVo = cfcaSignInfoService.queryByPk(inVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键删除信息
	 * 
	 * @param cfcaSignInfo
	 * @return
	 */
	@RequestMapping(value = "/cfca/sign/info", method = RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody CfcaSignInfoVO inCfcaSignInfoVo) {
		int outputVo = cfcaSignInfoService.deleteByPk(inCfcaSignInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 根据主键更新信息
	 * 
	 * @param inCfcaSignInfoVo
	 * @return
	 */
	@RequestMapping(value = "/cfca/sign/info", method = RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody CfcaSignInfoVO inCfcaSignInfoVo) {
		int outputVo = cfcaSignInfoService.updateByPk(inCfcaSignInfoVo);
		return getResponseData(outputVo);

	}

	/**
	 * 新增数据
	 * 
	 * @param inCfcaSignInfoVo
	 * @return
	 */
	@RequestMapping(value = "/cfca/sign/info", method = RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertCfcaSignInfo(@RequestBody CfcaSignInfoVO inCfcaSignInfoVo) {
		int outputVo = cfcaSignInfoService.insertCfcaSignInfo(inCfcaSignInfoVo);
		return getResponseData(outputVo);

	}
	
	/**
	 * 
	 * 需求编号：【】（业务提的需求编号）
	 * 问题编号：【3313/3312】（若不是因为修复问题而新增该类，则留空）
	 * 开发人员：【wanglh4】
	 * 创建日期：【2019年7月25日】
	 * 功能描述：前台页面数据展示及导出
	 */
	@RequestMapping(value = "/cfca/sign/allInfos", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<CfcaSignInfoVO>> queryCfcaSignInfoByCondition(@RequestBody CfcaSignInfoVO cfcaSignInfoVo) {
		List<CfcaSignInfoVO> outputVo = new ArrayList<CfcaSignInfoVO>();
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		cfcaSignInfoVo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		//当签章明细未配置数据权限时，默认查询本人所属机构
		dataRuleType = dataRuleTypeMap == null ? DataRuleType.CurrOrg : dataRuleType;
		setUserInfoToVO(cfcaSignInfoVo);
		outputVo = getInfos(cfcaSignInfoVo, dataRuleType);
		return getResponseData(outputVo);
	}


	/**
	 * 
	 * 需求编号：【】（业务提的需求编号）
	 * 问题编号：【3313/3312】（若不是因为修复问题而新增该类，则留空）
	 * 开发人员：【wanglh4】
	 * 创建日期：【2019年7月25日】
	 * 功能描述：根据页面传入的数据和查询合同表得到的合同号查询cfca加签信息
	 */
	private List<CfcaSignInfoVO> acquireCfcaInfosFrCtr(CfcaSignInfoVO cfcaSignInfoVo, List<CtrLoanContVO> ctrInfos, DataRuleType dataRuleType) {
		//从合同表查询结果里得到合同号
		List<String> eleContNo = ctrInfos.stream().map(a->a.getContNo()).collect(Collectors.toList());
		cfcaSignInfoVo.setEleContNo(eleContNo);
		List<CfcaSignInfoVO> rs = new ArrayList<CfcaSignInfoVO>();
		if(!StringUtil.isNullorBank(cfcaSignInfoVo.getExportFlag()) && "exp".equals(cfcaSignInfoVo.getExportFlag())) {
			logger.info("开始导出数据！");
			rs = cfcaSignInfoService.queryCfcaInfo(cfcaSignInfoVo, dataRuleType.getValue());
		} else if(StringUtil.isNullorBank(cfcaSignInfoVo.getExportFlag())) {
			logger.info("开始查询数据！");
			rs = cfcaSignInfoService.queryCfcaInfoSByPage(cfcaSignInfoVo, dataRuleType.getValue());
		}
		return rs;
	}
	
	/**
	 * 
	 * 需求编号：【】（业务提的需求编号）
	 * 问题编号：【3313/3312】（若不是因为修复问题而新增该类，则留空）
	 * 开发人员：【wanglh4】
	 * 创建日期：【2019年7月25日】
	 * 功能描述：根据页面传入的数据查询cfca加签信息并调用方法查询拼接合同信息
	 */
	private List<CfcaSignInfoVO> getInfos(CfcaSignInfoVO cfcaSignInfoVo, DataRuleType dataRuleType) {
		List<CfcaSignInfoVO> rs = new ArrayList<CfcaSignInfoVO>();
		if(!StringUtil.isNullorBank(cfcaSignInfoVo.getExportFlag()) && "exp".equals(cfcaSignInfoVo.getExportFlag())) {
			logger.info("开始导出数据！");
			rs = cfcaSignInfoService.queryCfcaSignInfoCondition(cfcaSignInfoVo, dataRuleType.getValue());
		} else if(StringUtil.isNullorBank(cfcaSignInfoVo.getExportFlag())) {
			logger.info("开始查询数据！");
			rs = cfcaSignInfoService.queryCfcaSignInfoConditionByPage(cfcaSignInfoVo, dataRuleType.getValue());
		}
		return rs;
	}
}
